package gov.fema.hazus.bdc.server.servlet;

import gov.fema.hazus.bdc.client.common.BuildingQuery;
import gov.fema.hazus.bdc.shared.entity.Building;

import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.googlecode.objectify.OKey;
import com.googlecode.objectify.OQuery;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

public class BuildingStatistics {

	private final Logger logger = Logger.getLogger(BuildingStatistics.class);
	//private DatastoreService datastore;
	private DAO dao = new DAO();
	
	public BuildingStatistics()
	{
		// datastore = DatastoreServiceFactory.getDatastoreService();
		
	}

	public int getCount()
	{
		Objectify ofy = ObjectifyService.begin();
		OQuery<Building> q = ObjectifyService.createQuery(Building.class);
		ArrayList<OKey<Building>> buildings = (ArrayList<OKey<Building>>) ofy.prepareKeysOnly(q).asList(FetchOptions.Builder.withLimit(Integer.MAX_VALUE).offset(0));
		return buildings.size();
	}
	
	public int getSearchCount(BuildingQuery query)
	{
		Objectify ofy = ObjectifyService.begin();
		OQuery<Building> q = ObjectifyService.createQuery(Building.class);
		q.sort("name"); // ascending sort on building name
		q.filter("terms == ", query.getSearchTerm().toUpperCase());
		ArrayList<Building> buildings = (ArrayList<Building>) ofy.prepare(q).asList(FetchOptions.Builder.withLimit(Integer.MAX_VALUE).offset(0));
		return buildings.size();
		
	}
	
}
